﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XingFuStudio.Pioneer.Net;
using XingFuStudio.Pioneer.Entity;
using XingFuStudio.Pioneer.Applications.City.Entity;
using XingFuStudio.Pioneer.Applications.City.Config;
using XingFuStudio.Pioneer.IO;
using FluorineFx.IO;
using XingFuStudio.Pioneer.Applications.City.Config.ConfigKeys;

namespace XingFuStudio.Pioneer.Applications.City.Operation {
    /// <summary>
    /// 执行各个基础设施的工作内容
    /// </summary>
    public class Execute  : OPBase{
        
        /// <summary>
        /// 创建业务对象
        /// </summary>
        /// <param name="httpManager"></param>
        /// <param name="account"></param>
        /// <param name="city"></param>
        /// <param name="config"></param>
        public Execute(HttpManager httpManager, AccountInfo account, CityInfo city, UserConfigSection config)
            : base(httpManager, account, city, config) {
        }

        /// <summary>
        /// 重写操作方法
        /// </summary>
        public override void Operate() {
            base.Operate();

            //初始化值为Null
            object value = null;
            //初始化字典是否有值标识
            bool hasValue = false;

            string message = String.Empty;

            try {
                //判断是否有值，并获取是否自动治病
                hasValue = config.ConfigItems.TryGetValue(ExecuteConfigKeys.AutoAid, out value);
                if (hasValue && Convert.ToBoolean(value)) {
                    for (int i = 0; i < 6; i++) {
                        PatientInfo patient = this.SpawnPatient();

                        if (patient != null) {
                            bool result = this.Aid(patient, out message);
                            this.DoPrintLog(true, message);
                        } else {
                            break;
                        }
                    }
                }

                //判断是否有值，并获取是否自动抓捕
                hasValue = config.ConfigItems.TryGetValue(ExecuteConfigKeys.AutoArrest, out value);
                if (hasValue && Convert.ToBoolean(value)) {
                    for (int i = 0; i < 6; i++) {
                        CriminalInfo criminal = this.SpawnCriminal();

                        if (criminal != null) {
                            bool result = this.Arrest(criminal, out message);
                            this.DoPrintLog(true, message);
                        } else {
                            break;
                        }
                    }
                }
            } catch (Exception ex) {
                this.DoError(ex);
            }
        }

        /// <summary>
        /// 产生病人
        /// </summary>
        /// <returns></returns>
        public PatientInfo SpawnPatient() {
            PatientInfo patient = null;

            //获取AMF数据
            AMFMessage message = this.GetAMFMessage("CCityFirstAid.callfunc", "spawnPatient");

            //解析数据
            Dictionary<string, object> content = (Dictionary<string, object>)message.Bodies[0].Content;
            Dictionary<string, object> data = (Dictionary<string, object>)content["data"];

            //获取数据是否成功的状态
            bool result = (string)data["result"] == "succ";
            if (result) {
                Dictionary<string, object> patientInfo = (Dictionary<string, object>)data["patientinfo"];

                patient = new PatientInfo();

                patient.PatientId = (string)patientInfo["pid"];
                patient.PatientName = (string)patientInfo["name"];
            }

            return patient;
        }

        /// <summary>
        /// 治疗
        /// </summary>
        /// <param name="patient"></param>
        /// <param name="resultMessage"></param>
        /// <returns></returns>
        public bool Aid(PatientInfo patient, out string resultMessage) {
            //获取AMF数据
            AMFMessage message = this.GetAMFMessage("CCityFirstAid.callfunc", "aid", patient.PatientId);

            //解析数据
            Dictionary<string, object> content = (Dictionary<string, object>)message.Bodies[0].Content;
            Dictionary<string, object> data = (Dictionary<string, object>)content["data"];

            //获取数据是否成功的状态
            bool result = (string)data["result"] == "succ";
            resultMessage = ((string)data["msg"]).NoHtml();

            return result;
        }

        /// <summary>
        /// 产生罪犯
        /// </summary>
        /// <returns></returns>
        public CriminalInfo SpawnCriminal() {
            CriminalInfo criminal = null;

            //获取AMF数据
            AMFMessage message = this.GetAMFMessage("CCityPoliceStation.callfunc", "spawnCriminal");

            //解析数据
            Dictionary<string, object> content = (Dictionary<string, object>)message.Bodies[0].Content;
            Dictionary<string, object> data = (Dictionary<string, object>)content["data"];

            //获取数据是否成功的状态
            bool result = (string)data["result"] == "succ";
            if (result) {
                Dictionary<string, object> criminalInfo = (Dictionary<string, object>)data["criminal"];

                criminal = new CriminalInfo();

                criminal.CriminalId = Convert.ToInt32((string)criminalInfo["cid"]);
                criminal.CriminalName = (string)criminalInfo["name"];
                criminal.ArrestId = Convert.ToInt64((string)criminalInfo["arrestid"]);
            }

            return criminal;
        }

        /// <summary>
        /// 抓捕罪犯
        /// </summary>
        /// <param name="criminal"></param>
        /// <param name="resultMessage"></param>
        /// <returns></returns>
        public bool Arrest(CriminalInfo criminal, out string resultMessage) {
            //查找警署
            var policeStations = this.city.Elements.Where(s => s.ElementId == 127);

            bool result = false;

            if (policeStations.Count() > 0) {
                //获取AMF数据
                AMFMessage message = this.GetAMFMessage("CCityPoliceStation.callfunc", "tryArrest",
                    criminal.ArrestId, policeStations.First().OrderId);

                //解析数据
                Dictionary<string, object> content = (Dictionary<string, object>)message.Bodies[0].Content;
                Dictionary<string, object> data = (Dictionary<string, object>)content["data"];

                //获取数据是否成功的状态
                result = (string)data["result"] == "succ";
                resultMessage = ((string)data["msg"]).NoHtml();
            } else {
                resultMessage = "抓捕失败，请选建设警署！";
            }
            return result;
        }
    }
}
